package com.lsy.weizhi2.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

public class MySetting extends MyBaseActivity implements View.OnClickListener{
    private Context context;
    private MyTitleView setting_title;
    private TextView setting_complete,setting_nickname,setting_phone,setting_head,setting_password,setting_logout,setting_version;
    private int xx;
    private Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_setting);
        context=MySetting.this;
        findView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        xx = 0;
        ViewTreeObserver vto = setting_title.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(setting_title.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                setting_title.setLayoutParams(params);
                            }
                        }
                    });
                }
            }
        });
    }

    private void findView() {
        setting_title= (MyTitleView) findViewById(R.id.setting_title);
        setting_complete= (TextView) findViewById(R.id.setting_complete);
        setting_nickname= (TextView) findViewById(R.id.setting_nickname);
        setting_phone= (TextView) findViewById(R.id.setting_phone);
        setting_head= (TextView) findViewById(R.id.setting_head);
        setting_password= (TextView) findViewById(R.id.setting_password);
        setting_logout= (TextView) findViewById(R.id.setting_logout);
        setting_version = (TextView) findViewById(R.id.setting_version);
        setting_complete.setOnClickListener(this);
        setting_nickname.setOnClickListener(this);
        setting_phone.setOnClickListener(this);
        setting_head.setOnClickListener(this);
        setting_password.setOnClickListener(this);
        setting_logout.setOnClickListener(this);
        setting_version.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.setting_complete:
                MyMethod.toActivity(context,CompleteInfo.class);
                break;
            case R.id.setting_nickname:
                MyMethod.toActivity(context,Setting_nickName.class);
                break;
            case R.id.setting_phone:
                MyMethod.toActivity(context,Setting_Phone.class);
                break;
            case R.id.setting_head:
                MyMethod.toActivity(context,Setting_Head.class);
                break;
            case R.id.setting_password:
                MyMethod.toActivity(context,Setting_Password.class);
                break;
            case R.id.setting_logout:
                Logout();
                break;
            case R.id.setting_version:
                MyMethod.toActivity(context,WebActivity2.class);
                break;
        }
    }

    private void Logout(){
        MyOkHttpUtils.getInstance(context).doGet(Constants.USER_LOGOUT, null)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        MyMethod.toActivity(context, LoginActivty.class);
                        MyInfo.User_Id=0;
                        MyInfo.Avatar=0;
                        MyInfo.Token=null;
                        SharedPreferences sharedPreferences = context.getSharedPreferences(MyInfo.User_info, 0);
                        sharedPreferences.edit().clear().commit();
                        ((Activity) context).finish();
                    }
                });
    }
}
